package com.feituan.core.persistence.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feituan.core.persistence.mapper.IntegrationProductMapper;
import com.feituan.core.persistence.mapper.IntegrationRecordMapper;
import com.feituan.core.persistence.mapper.IntegrationUserMapper;
import com.feituan.core.persistence.mapper.OrderMapper;
import com.feituan.core.persistence.service.IIntegrationUserService;
import com.feituan.core.persistence.service.IOrderService;
import com.feituan.core.pojo.entity.IntegrationProduct;
import com.feituan.core.pojo.entity.IntegrationRecord;
import com.feituan.core.pojo.entity.IntegrationUser;
import com.feituan.core.pojo.entity.Order;
import com.feituan.core.pojo.response.IntegrationUserRes;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IntegrationUserServiceImpl extends ServiceImpl<IntegrationUserMapper, IntegrationUser> implements IIntegrationUserService {


    @Resource
    private IntegrationRecordMapper integrationRecordMapper;

    @Resource
    private IntegrationProductMapper integrationProductMapper;

    @Resource
    private OrderMapper orderMapper;
    /**
     * 新增用户积分
     *
     * @param integrationUser
     * @return
     */
    @Override
    public int saveUserIntegration(IntegrationUser integrationUser) {
        return this.getBaseMapper().insert(integrationUser);
    }

    /**
     * 查询用户积分明细
     *
     * @param userId userId
     * @return
     */
    @Override
    public List<IntegrationUserRes> selectIntegrationByUserId(Long userId) {
        LambdaQueryWrapper<IntegrationUser> qw = new LambdaQueryWrapper<>();
        qw.eq(true, IntegrationUser::getUserId, userId);
        List<IntegrationUser> integrationUsers = this.getBaseMapper().selectList(qw);

        List<IntegrationUserRes> integrationUserResList = new ArrayList<>();
        integrationUsers.forEach(el -> {
            IntegrationUserRes integrationUserRes = new IntegrationUserRes();
            BeanUtils.copyProperties(el, integrationUserRes);
            // 查询订单
            Order order = orderMapper.selectById(el.getOrderId());
            integrationUserRes.setOrder(order);
            integrationUserResList.add(integrationUserRes);
        });
        return integrationUserResList;
    }




}
